package org.cvcsc.weproject.service;

import org.cvcsc.weproject.domain.Student;
import org.cvcsc.weproject.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Service;

@Service
public class AuthenticationService {

    @Autowired
    private StudentRepository studentRepository;

    public Student authenticate(String number, String password) throws AuthenticationException {
        Student student = studentRepository.findByStudentNameNumber_Number(number);
        if (student == null || !student.verifyPassword(password)) {
            throw new MyAuthenticationException("Invalid credentials");
        }
        return student;
    }

}
class MyAuthenticationException extends AuthenticationException {
    public MyAuthenticationException(String msg) {
        super(msg);
    }
}